﻿using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{

    public static GameController gameController;
    public Transform rotateTarget;
    public Transform moveTarget;
    public Transform player;
    public Transform monsterController;
    public Transform prefabBean;
    public Transform prefabBeanTarget;
    public Transform PrefabMonster;
    public Quaternion forward;
    public Quaternion back;
    public Quaternion left;
    public Quaternion right;
    public int maxWidth;
    public int maxHeight;
    public int score;
    public int beanNumber;

    void Awake ()
    {
        gameController = this;
    }

    // Use this for initialization
    void Start ()
    {
        BigBean.scale = 0.5f;
        StartCoroutine ("BigBeanScale");
        StartCoroutine ("PutBean");
    }

    //重试
    public void ReTry ()
    {
        Application.LoadLevel (1);
    }

    //增加豆子数
    public void AddBean ()
    {
        beanNumber++;
    }

    //减少豆子数
    public void DestroyBean ()
    {
        beanNumber--;
    }

    //吃豆子
    public void EatBean ()
    {
        beanNumber--;
        gameController.score += 1;
        if (beanNumber == 0)
        {
            Win ();
        }
    }

    //吃大豆
    public void EatBigBean ()
    {
        gameController.score += 10;
        monsterController.gameObject.GetComponent<MonsterController> ().Dangers ();
    }

    //游戏胜利
    public void Win ()
    {
        GlobalManager.level++;
        GlobalManager.win = true;
        GlobalManager.score += score;
        Application.LoadLevel (2);
        Debug.Log ("win");
    }

    //游戏失败
    public void Lose ()
    {
        GlobalManager.win = false;
        Application.LoadLevel (2);
    }

    //豆子动画
    IEnumerator BigBeanScale ()
    {
        int i;
        i = 1;
        while (true)
        {
            if (BigBean.scale >= 0.5f)
            {
                i = -1;
            }
            if (BigBean.scale <= 0.2f)
            {
                i = 1;
            }
            
            BigBean.scale += Time.deltaTime * i * 0.4f;
            yield return 0;
        }
    }

    //放豆子
    IEnumerator PutBean ()
    {
        int z;
        for (z = 0; z <= maxHeight; z++)
        {
            Instantiate (prefabBeanTarget, new Vector3 (0, 0, z), this.transform.rotation);
            yield return 0;
        }
 
    }

}
